package com.chinawayltd.ews.sample.web.algorithm;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.model.TDriverLogin;
import com.chinawayltd.ews.sample.service.algorithm.DriverLoginService;
import com.chinawayltd.ews.sample.util.StringUtils;
import com.chinawayltd.ews.sample.web.query.DriverLoginGirdQuery;
import com.chinawayltd.ews.sample.util.ResultModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @program: greeProject
 * @description 司机APP管理
 * @author: chenxin
 * @create: 2018年4月11日 下午10:59:20
 */
@Api(tags = { "司机APP管理API" })
@RestController
@RequestMapping(value = "/web/driver/login")
public class DriverLoginController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	DriverLoginService driverLoginService;

    @ApiOperation(value = "列表查询接口")
    @RequestMapping(value = "/loadGirdData", method = { RequestMethod.POST })
    public ResultModel loadGirdData(DriverLoginGirdQuery queryParam) {
    	ResultModel model = new ResultModel();
        try {
            List<TDriverLogin> list = driverLoginService.list(queryParam);
            model.setCode(0);
			model.setSub_code(0);
            model.setData(new JSONObject().fluentPut("result", list));
            model.setPage(queryParam);
        }catch (Exception e){
        	model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", "查询发生异常"));
			logger.error("站点列表查询出错！", e);
        }
        return model;
    }
	
	@ApiOperation(value = "司机APP信息根据ID查询接口")
	@RequestMapping(value = "/find/{id}", method = { RequestMethod.GET })
	@ApiImplicitParam(paramType = "path", name = "id", value = "司机APP信息ID", dataType = "Long", required = true)
	public ResultModel getById(@PathVariable Long id) {
		ResultModel model = new ResultModel();
		try {
			TDriverLogin data = driverLoginService.getById(id);
			model.setData(new JSONObject().fluentPut("result", data));
        }catch (Exception e){
        	model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", "查询发生异常"));
        	logger.error("根据ID查询信息出错！id：" + id, e);
        }
		return model;
	}

	@ApiOperation(value = "通过登录账号查询app用户,可用来校验登录账号唯一性", notes = "校验名称：添加校验登录账号不需要ID,修改校验登录账号必要ID,返回Sub_code=0为可用,1为已存在!")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "driverId", value = "驾驶员ID", dataType = "Long", required = false),
		@ApiImplicitParam(paramType = "query", name = "loginName", value = "登录账号", dataType = "String", required = true)})
	@RequestMapping(value = { "/getDriverLoginByNameAndId" }, method = RequestMethod.POST)
	public ResultModel getDriverLoginByNameAndId(Long driverId, String loginName){
		ResultModel model = new ResultModel();
		try {
			List<TDriverLogin> list = driverLoginService.getDriverLoginByNameAndId(driverId, loginName);
			if(list.size() > 0){
				model.setData(new JSONObject().fluentPut("result", "不可用"));
			}else{
				model.setData(new JSONObject().fluentPut("result", "可用"));
			}
		} catch (Exception e) {
			logger.error("查询失败！", e);
			model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", "查询发生异常"));
		}
		return model;
	}
	
	@ApiOperation(value = "司机APP信息新增接口")
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	@ApiImplicitParams({
    	@ApiImplicitParam(paramType="query", name="driverId",value="驾驶员ID",dataType="Long",required=false),
    	@ApiImplicitParam(paramType="query", name="loginName",value="登录账号",dataType="String",required=true),
    	@ApiImplicitParam(paramType="query", name="password",value="账号密码",dataType="String",required=true)
    })
	public ResultModel add(TDriverLogin tDriverLogin) {
		ResultModel model = new ResultModel();
		try {
			if(StringUtils.isBlank(tDriverLogin.getDriverId().toString())){
				throw new RuntimeException("添加失败！");
			}
			// 为null也过滤了重复添加操作
			ResultModel rm  = this.getDriverLoginByNameAndId(null, tDriverLogin.getLoginName());
			if(rm.getSub_code()==1){
				return new ResultModel(0, 1, new JSONObject().fluentPut("sub_msg" , "添加失败"));
			}
			int result = driverLoginService.add(tDriverLogin);
			if(result > 0){
            	model.setData(new JSONObject().fluentPut("result" , "添加成功"));
	        }else{
	        	model.setSub_code(1);
	        	model.setData(new JSONObject().fluentPut("sub_msg" , "添加失败"));
	        }
        }catch (Exception e){
        	model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", "司机APP信息新增发生异常"));
        	logger.error("司机APP信息新增发生异常", e);
        }
		return model;
	}

	@ApiOperation(value = "司机APP信息修改接口")
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	@ApiImplicitParams({
    	@ApiImplicitParam(paramType="query", name="driverId",value="驾驶员ID",dataType="Long",required=true),
    	@ApiImplicitParam(paramType="query", name="loginName",value="登录账号",dataType="String",required=true),
    	@ApiImplicitParam(paramType="query", name="password",value="账号密码",dataType="String",required=false)
    })
	public ResultModel update(TDriverLogin tDriverLogin) {
		ResultModel model = new ResultModel();
		try {
			if(StringUtils.isBlank(tDriverLogin.getDriverId().toString())){
				throw new RuntimeException("修改失败！");
			}
			ResultModel rm  = this.getDriverLoginByNameAndId(tDriverLogin.getDriverId(), tDriverLogin.getLoginName());
			if(rm.getSub_code()==1){
				return new ResultModel(0, 1, new JSONObject().fluentPut("sub_msg" , "修改失败"));
			}
			int result = driverLoginService.update(tDriverLogin);
			if(result > 0){
            	model.setData(new JSONObject().fluentPut("result" , "修改成功"));
	        }else{
	        	model.setSub_code(1);
	        	model.setData(new JSONObject().fluentPut("sub_msg" , "修改失败"));
	        }
        }catch (Exception e){
        	model.setSub_code(1);
        	model.setData(new JSONObject().fluentPut("sub_msg" , "修改失败"));
        	logger.error("司机APP信息修改发生异常", e);
        }
		return model;
	}

	@ApiOperation(value = "司机APP信息删除接口")
	@RequestMapping(value = "/delete/{id}", method = { RequestMethod.DELETE })
	@ApiImplicitParam(paramType = "path", name = "id", value = "司机APP信息ID", dataType = "Long", required = true)
	public ResultModel delete(@PathVariable Long id) {
		ResultModel model = new ResultModel();
        try {
        	int result = driverLoginService.delete(id);
        	if(result > 0){
            	model.setData(new JSONObject().fluentPut("result" , "删除成功"));
	        }else{
	        	model.setSub_code(1);
	        	model.setData(new JSONObject().fluentPut("sub_msg" , "删除失败"));
	        }
		} catch (Exception e) {
			model.setSub_code(1);
			model.setData(new JSONObject().fluentPut("sub_msg", "司机APP信息删除发生异常"));
			logger.error("司机APP信息删除发生异常！id="+id, e);
		}
		return model;
	}

}
